package com.boarsoft.boar.batch.demo;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.boarsoft.flow.core.SimpleFlowEngine;
import com.boarsoft.rpc.core.RpcCore;

import ch.qos.logback.core.joran.spi.JoranException;

public class BatchDemoMain {
	private static Logger log = LoggerFactory.getLogger(BatchDemoMain.class);

	// default String a;

	public static void main(String[] args) throws NumberFormatException, JoranException, IOException {
		final ClassPathXmlApplicationContext ctx = //
				new ClassPathXmlApplicationContext("classpath:conf/context.xml");

		Runtime.getRuntime().addShutdownHook(new Thread() {
			public void run() {
				try {
					RpcCore.getCurrentInstance().shutdown();
				} catch (Exception e) {
					log.error("Shutdown hook can not close application context", e);
				} finally {
					ctx.close();
				}
			}
		});

		System.out.println("Startup successfully.");

		// demo1(ctx);
		// bigFileLoadgFile(ctx);
		// dpfm01Query(ctx);
		// dpfm02Query(ctx);
		
//		BigLoader bl = ctx.getBean("bigLoader", BigLoader.class);
//		long l = System.currentTimeMillis();
//		try {
//			bl.process(null, null, null);
//		} catch (Exception e) {
//			log.error("Error on load big file", e);
//			System.exit(-1);
//		}
//		System.out.println(System.currentTimeMillis() - l);
//		System.exit(0);

//		demo1(ctx);
//		bigFileLoadgFile(ctx);
//		dpfm01Query(ctx);
//		dpfm02Query(ctx);
	}

	public static void demo1(ApplicationContext ctx) {
		Map<String, Object> pm = new HashMap<String, Object>();
		pm.put("pageCount", new Integer(100));
		SimpleFlowEngine fe = (SimpleFlowEngine) ctx.getBean("simpleFlowEngine");
		try {
			fe.start("demo1", pm);
			log.info("............................");
		} catch (Throwable e1) {
			e1.printStackTrace();
		}
	}

	public static void bigFileLoadgFile(ApplicationContext ctx) {
		System.out.println("进入bigFileLoadgFile.");
		
		Map<String, Object> pm = new HashMap<String, Object>();
		pm.put("commitCount", new Integer(1000));
		SimpleFlowEngine fe = (SimpleFlowEngine) ctx.getBean("simpleFlowEngine");
		try {
			// fe.start("LoadDpfm01", pm);
			fe.start("LoadDpfm02", pm);
			log.info(".............done...............");
		} catch (Throwable e1) {
			e1.printStackTrace();
		}
	}

	public static void dpfm01Query(ApplicationContext ctx) {
		Map<String, Object> pm = new HashMap<String, Object>();
		pm.put("select",
				"select id, DM01ACCT," + "  DM01CLSFLG,  DM01DOCLST,  DM01PSWLST,  DM01ACTFLG,  DM01SECFLG,"
						+ "  DM01DOCFLG,  DM01VOUCLS,  DM01VOUNUM,  DM01WTTYPE,  DM01WTPRF1,"
						+ "  DM01INQPAS,  DM01WTPRF3,  DM01PRTNUM,  DM01UPRTNU,  DM01ACCNUM,"
						+ "  DM01ACCSEQ,  DM01ORGNO,  DM01CLIPRO,  DM01PBNUM,  DM01LICNO,"
						+ "  DM01AUTNO,  DM01AVLDAT,  DM01FLAG,  DM01NODE,  LAST_ETL_ACG_DT,  DEL_F" + " from Coredpfm01 ");
		// + "where DM01ACCT = '10618000001054537 ' "
		// + " and DM01WTPRF3 ='9106181000000000176'");
		pm.put("tenantCount", 2);
		SimpleFlowEngine fe = (SimpleFlowEngine) ctx.getBean("simpleFlowEngine");
		try {
			fe.start("dpfm01Query", pm);
			log.info("............................");
		} catch (Throwable e1) {
			e1.printStackTrace();
		}
	}

	public static void dpfm02Query(ApplicationContext ctx) {
		Map<String, Object> pm = new HashMap<String, Object>();
		pm.put("select",
				"select id,DM02ACCT,DM02ACCS,DM02ACCTYP,DM02HDFLG,DM02CLSFLG,"
						+ "DM02CCY,DM02CSHTRF,DM02PRDID,DM02SBNO,DM02OPNDAT,DM02STRDAT,DM02ENDDAT,"
						+ "DM02TERM1,DM02TERM2,DM02BALE,DM02BALD,DM02BALEN,DM02EBALD,DM02LBALE,"
						+ "DM02LBALD,DM02CTLBAL,DM02FRZBAL,DM02OVDBAL,DM02CVDB1,DM02CVDB2,DM02CVDB3,"
						+ "DM02ITPLAN,DM02ITFLT,DM02LSTDTE,DM02NODE,DM02ACCCHR,DM02ACCCH1,DM02YZFLG,"
						+ "DM02TDUCTL,DM02CSHFLG,DM02ADVTIM,DM02DMOD,DM02INTRAT,DM02FRZFLG,DM02MRTFLG,"
						+ "DM02PRVFLG,DM02PBLIN,DM02CVNFLG,DM02SECFLG,DM02OVDFLG,DM02IINFLG,DM02INTFLG,"
						+ "DM02INTFLG1,DM02NRAMT,DM02PMSAMT,DM02INTPRD,DM02DONTIM,DM02NXDAT,DM02LZFLG,"
						+ "DM02LIEID,DM02DTFLG,DM02ZCTIM,DM02CONNO,DM02FEEFLG,DM02REMARK,DM02OLBAL,"
						+ "DM02FLAG,DEL_F,LAST_ETL_ACG_DT" + " from Coredpfm02 ");
		// + "where DM02ACCT = '10618000001054537 ' "
		// + " ");
		pm.put("tenantCount", 2);
		SimpleFlowEngine fe = (SimpleFlowEngine) ctx.getBean("simpleFlowEngine");
		try {
			fe.start("dpfm02Query", pm);
			log.info("............................");
		} catch (Throwable e1) {
			e1.printStackTrace();
		}
	}

}
